// Includes
#include "Timer.h"

#include <SDL/SDL.h>		// SDL main header

namespace EraAstral
{
	namespace core
	{
		// Timer class constructor
		Timer::Timer()
			: IKernelComponent(false)
		{
			m_isRunning = false;
			m_ticksTotal = 0;
			m_ticksSinceLast = 0;
			m_ticksStart = 0;
		}
		
		// Timer class destructor
		Timer::~Timer()
		{
		}
		
		// Update logic
		void Timer::Update(unsigned int ticks)
		{
			// Check if timer was started
			if (m_isRunning)
			{
				// Calculate number of ticks since last update
				m_ticksSinceLast = SDL_GetTicks() - m_ticksTotal;
				// Find total number of ticks
				m_ticksTotal = SDL_GetTicks() - m_ticksStart;
			}
		}
		
		// Starts the timer
		void Timer::Start()
		{
			// Set running flag
			m_isRunning = true;
			// Get SDL ticks
			m_ticksStart = SDL_GetTicks();
			// Reset tick values
			m_ticksTotal = 0;
			m_ticksSinceLast = 0;
		}
		
		// Stops the timer
		void Timer::Stop()
		{
			// Clear running flag
			m_isRunning = false;
		}
		
		// Force the timer to sleep
		void Timer::Sleep(unsigned int miliseconds)
		{
			SDL_Delay(miliseconds);
		}
		
		// Returns true if the timer has been started
		bool Timer::IsRunning()
		{
			return m_isRunning;
		}
		
		// Returns total number of ticks
		unsigned int Timer::GetTicksTotal()
		{
			return m_ticksTotal;
		}
		
		// Gets number of ticks since last update
		unsigned int Timer::GetTicksSinceLast()
		{
			return m_ticksSinceLast;
		}
	}
}
